/**
 *    Copyright (C) 2009  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

#ifndef _CUBEFS_CORE_API_TOOLS_PLUGIN_HPP
#define	_CUBEFS_CORE_API_TOOLS_PLUGIN_HPP

#include <cubefs/api/exceptions.hpp>
#include <cubefs/api/tools/plugin.hpp>
#include <cubefs/core/tools/plugin.hpp>
#include <cubefs/core/tools/configurator.hpp>
#include <cubefs/core/api/reflection/type.hpp>

#define CUBEFS_SOURCE "cubefs/core/api/tools/plugin.hpp:" CUBEFS_AT

namespace cubefs    {
namespace api       {

using cubefs::tools::TrimString;

template <typename Interface>
class PluginImplT: public TypedImplT<Interface> {

	typedef PluginImplT<Interface> 	Me;
    typedef TypedImplT<Interface> 	Base;

public:
    PluginImplT(cubefs::tools::Plugin* plugin): Base(), plugin_(plugin), configuration_(&plugin->GetConfig()) {}

    virtual ~PluginImplT() throw() {
    	delete plugin_;
    }

    virtual const Configuration* GetGonfiguration() const {
    	return &configuration_;
    }

    virtual bool IsPropertyDefined(StringRef name) const {
    	return configuration_.IsPropertyDefined(name);
    }

    virtual String GetProperty(StringRef name) const {
    	return configuration_.GetProperty(name);
    }

    virtual String GetResourcePath() const {
    	plugin_->GetResourcePath();
    }

    virtual Version GetVersion() const {
    	plugin_->GetVersion();
    }

    virtual Version GetApiVersion() const {
    	plugin_->GetApiVersion();
    }

    virtual SharedLibrary& GetLibrary() {
    	return plugin_->GetLibrary();
    }

    static void InitType()
    {
    	type_ = new AbstractType(TypeNameFactory<Interface>::name(), Typed::MyType());
    }

    static void InitOperations()
    {
    	AbstractType *type = static_cast<AbstractType*>(type_);

    	type->AddOperation(new BasicOperation<bool (Me::*)(String), bool (Me::*)(StringRef) const>(type, "IsPropertyDefined", "name", &Me::IsPropertyDefined));
    	type->AddOperation(new BasicOperation<String (Me::*)(String), String (Me::*)(StringRef) const>(type, "GetProperty", "name", &Me::GetProperty));

    	type->AddOperation(new BasicOperation<String (Me::*)(), String (Me::*)() const>(type, "GetResourcePath", &Me::GetResourcePath));
    	type->AddOperation(new BasicOperation<Version (Me::*)(), Version (Me::*)() const>(type, "GetVersion", &Me::GetVersion));
    	type->AddOperation(new BasicOperation<Version (Me::*)(), Version (Me::*)() const>(type, "GetApiVersion", &Me::GetApiVersion));
    }


    CUBEFS_DECLARE_REFLECTION;

private:
    cubefs::tools::Plugin* plugin_;
    ConfigurationImpl configuration_;
};


template <typename Interface>
Type* PluginImplT<Interface>::type_						= NULL;

typedef PluginImplT<Plugin>								PluginImpl;



}
}

#undef CUBEFS_SOURCE
#endif
